var choosePage = function () {
};
$(function () {
    var _goodsinfo, _sku_specs = [], _skuidresult = -1;
    choosePage = function () {

    };

    choosePage.prototype = {
        // 初始化页面
        init: function (goodsinfo) {
            _goodsinfo = goodsinfo;
            this.loaddata();
        },
        // 加载商品列表数据
        loaddata: function () {
            var model = this;
            $.ajax({
                type: 'GET',
                url: CONFIG_MANAGER.RequstUrl + "v1/goods/spec/" + _goodsinfo.sku_id
            }).done(function (res) {
                model.refactorSpecs(res.sku_specs);
                if (_sku_specs.length > 1) {
                    _skuidresult = _sku_specs[1];
                }
                model.paintpage(res.allspecs);
                model.bindEvent();
            });
        },

        //  画评论页面
        paintpage: function (allspecs) {
            var html = [];
            html.push("<div class='cpxqxzbg moveFromBottom' id='choosePage' style='display:none;'>");
            html.push("<div class='choose-title mbbottom'><p class='choose-tu'><img src='" + getImageUrl(_goodsinfo.logo, 496, 466) + "' src='../../images/place/496x466.png'></p>");
            html.push("<div class='choose-price'><p class='priceContainer'>" + _goodsinfo.name + _goodsinfo.title + "</p><p class='stock-control'>￥" + _goodsinfo.shop_price + "</p>");
            html.push("</div>");
            html.push("<p class='ob-cha' id='btnclosechoose'><i class='iconfont icon-diancha'></i></p>");
            html.push("</div>");

            html.push("<div class='choose-content '>");
            for (var por in allspecs) {
                var specslist = allspecs[por].split(',');
                var specsformat = [];
                for (var j = 0; j < specslist.length; j++) {
                    var selectedclass = (j == 0) ? "class='ysxzs'" : "";
                    specsformat.push("<span " + selectedclass + ">" + specslist[j] + "</span>")
                }
                html.push("<div class='yansexz mbbottom'><p class='ysbiaoti'>" + por + "</p><p class='ysnrxz' name='chooseitemlist'>" + specsformat.join("") + "</p></div>");
            }
            html.push("<div class='shuliang mbbottom'><p class='ysbiaoti'>数量</p><p class='gwslny clear'><span class='mbright1' id='btnminus'>-</span><span class='slsrk' id='goodsnum'>1</span><span class='mbleft' id='btnadd'>+</span></p></div>");
            html.push("</div>");
            html.push("<p class='chooose-confirm' id='btnchooseconfirm'>确定</p>");
            html.push("</div>");
            html.push("<div class='dingwei' id='choosebgdiv' style='display:none;z-index: 3'></div>")

            $("body").append(html.join(""));
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 关键选择页面
            $("#btnclosechoose,#choosebgdiv").on("tap", function () {
                model.hide();
            });

            // 选择颜色
            $("#goodscolorlist span").on("tap", function () {
                // 库存不足
                if ($(this).hasClass("xzwbg")) {
                    $.toast("暂无存货！");
                    return;
                }
                var prev = $("#goodscolorlist span[class=ysxzs]");
                if (prev) {
                    prev.removeClass();
                }
                $(this).addClass("ysxzs");
            });

            // 选择规格
            $("p[name=chooseitemlist]").on("tap", "span", function () {
                var liitem = $(this);
                var origliitem = liitem.parents("p[name=chooseitemlist]").find("span[class=ysxzs]");

                // 选中样式
                if (liitem.hasClass("hover")) {
                    return;
                } else {
                    origliitem.removeClass("ysxzs");
                    liitem.addClass("ysxzs");

                    _skuidresult = model.checkinventory();
                    if (_skuidresult == -1) {
                        $.toast("暂无存货！");
                        origliitem.addClass("ysxzs");
                        liitem.removeClass("ysxzs");
                    }
                }
            });

            // 数量减
            $("#btnminus").on("tap", function () {
                var goodsnum = parseInt($("#goodsnum").text());
                if (goodsnum == 1) {
                    return;
                }

                $("#goodsnum").text((goodsnum - 1));
            });
            // 数量加
            $("#btnadd").on("tap", function () {
                var goodsnum = parseInt($("#goodsnum").text());
                //if (goodsnum == 1) { return; }

                $("#goodsnum").text((goodsnum + 1));
            });

            // 确定
            $("#btnchooseconfirm").on("tap", function () {
                model.confirm(_skuidresult, parseInt($("#goodsnum").text()));
                model.hide();
            });
        },

        // 重构数组
        refactorSpecs: function (sku_specs) {
            for (var i = 0; i < sku_specs.length; i++) {
                var spce = sku_specs[i];
                var specskey = [];
                for (var prop in spce.spec_item) {
                    specskey.push(prop + "-" + spce.spec_item[prop]);
                }

                _sku_specs.push(specskey.join("-"));
                _sku_specs.push(spce.skuid);
            }
        },

        // 判断是否有存货
        checkinventory: function () {
            var selectedspec = $("p[name=chooseitemlist]").find('span[class=ysxzs]');
            var selectedspeckey = [];
            for (var i = 0; i < selectedspec.length; i++) {
                var spec = $(selectedspec[i]);
                var specname = spec.parents('p[name=chooseitemlist]').prev().text();
                selectedspeckey.push(specname + "-" + spec.text())
            }
            var index = _sku_specs.indexOf(selectedspeckey.join("-"));
            if (index > -1) {
                return _sku_specs[index + 1];
            }
            return -1;
        },
        // 确定
        confirm: function (result, qte) {
        },
        // 显示
        show: function () {
            this.isdisplay = true;
            $("#choosebgdiv").show();
            $("#choosePage").show();
            setTimeout(function () {
                $("#choosePage").addClass("moveFromBottom-moved");
            }, 100);
        },
        // 隐藏
        hide: function () {
            this.isdisplay = false;
            $("#choosePage").removeClass("moveFromBottom-moved");
            ;
            setTimeout(function () {
                $("#choosebgdiv").hide();
                $("#choosePage").hide();
            }, 200);
        },
        isdisplay: false
    };

});
